﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using YwangHRMSys.DAL;
using YwangHRMSys.Model;


namespace YwangHRMSys.UI.EmployeeMgr.AddressInfoMgr
{
    /// <summary>
    /// AddressEditWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddressEditWindow : Window
    {
        public AddressEditWindow()
        {
            InitializeComponent();
        }
        public Guid EditGuid { get; set; }


        /// <summary>
        /// 自动加载员工信息
        /// </summary>
            private void LoadData()
            {
                EmployeeInfoModel model = new EmployeeInfoDAL().GetById(EditGuid);
                string editName = model.Name;
                LabelEmployeeName.Content = editName + "的地址信息修改";
         
                //地址信息的加载
                DomicilebProvList.ItemsSource = new AreaFullInfoDAL().ListProvAll();
                NowProvList.ItemsSource = new AreaFullInfoDAL().ListProvAll();
                //户籍信息
                EmployeeAddressInfoModel domicileAddressIdModel = new EmployeeAddressInfoDAL().GetByEmployeeAddressInfoId
                    (model.DomicileAddressId);
                //现在信息
            EmployeeAddressInfoModel nowdAddressInfoModel = new EmployeeAddressInfoDAL().GetByEmployeeAddressInfoId
                    (model.CurrentAddressId);
        
            DomicilebProvList.Text = domicileAddressIdModel.Provinces;
            DomicileCityList.Text = domicileAddressIdModel.City;
            DomicileCountyList.Text = domicileAddressIdModel.County;
            TxtDomicile.Text = domicileAddressIdModel.Address;

                //
            NowProvList.Text = nowdAddressInfoModel.Provinces;
            NowCityList.Text = nowdAddressInfoModel.City;
            NowCountyList.Text = nowdAddressInfoModel.County;
            TxtCurrentAddress.Text = nowdAddressInfoModel.Address;
            }


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           LoadData();
        }
        
        private void DomicilebProvList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
         
         
            if (DomicilebProvList != null)
            {
                if (DomicilebProvList.SelectedValue != null)
                {
                    int id = (int)DomicilebProvList.SelectedValue;
                    DomicileCityList.ItemsSource = new AreaFullInfoDAL().GetByAreaFullInfoId(id);
                }
            }

        }

        private void DomicileCityList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (DomicileCityList != null)
            {
                if (DomicileCityList.SelectedValue != null)
                {
                    int id = (int)DomicileCityList.SelectedValue;
                    DomicileCountyList.ItemsSource = new AreaFullInfoDAL().GetByAreaFullInfoId(id);
                }
            }
        }

        private void NowProvList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (NowProvList.SelectedValue != null)
            {
                int id = (int)NowProvList.SelectedValue;
                NowCityList.ItemsSource = new AreaFullInfoDAL().GetByAreaFullInfoId(id);
            }

        }

        private void NowCityList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (NowCityList.SelectedValue != null)
            {
                int id = (int)NowCityList.SelectedValue;
                NowCountyList.ItemsSource = new AreaFullInfoDAL().GetByAreaFullInfoId(id);
            }
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            EmployeeInfoModel model = new EmployeeInfoDAL().GetById(EditGuid);
            var modelDomicile = new EmployeeAddressInfoModel();
            modelDomicile.Id = model.DomicileAddressId;
            modelDomicile.Provinces = DomicilebProvList.Text;
            modelDomicile.City = DomicileCityList.Text;
            modelDomicile.County=DomicileCountyList.Text;
            modelDomicile.Address=  TxtDomicile.Text ;
            modelDomicile.AddressAllInfo = DomicilebProvList.Text + DomicileCityList.Text + DomicileCountyList.Text+TxtDomicile.Text;

            var modelNow = new EmployeeAddressInfoModel();
            modelNow.Id = model.CurrentAddressId;
            modelNow.Provinces=NowProvList.Text ;
           modelNow.City=   NowCityList.Text;
            modelNow.County=  NowCountyList.Text ;
           modelNow.Address=   TxtCurrentAddress.Text;
            modelNow.AddressAllInfo = NowProvList.Text + NowCityList.Text + NowCountyList.Text+TxtCurrentAddress.Text;

            new EmployeeAddressInfoDAL().UpdateEmployeeAddressInfo(modelDomicile);
            new EmployeeAddressInfoDAL().UpdateEmployeeAddressInfo(modelNow);
            DialogResult = true;
        }
    }
}
